/**
 * Created by dywu on 2015/9/24.
 */
angular.module('service.fileupload', ['ngResource'])
    .service('FileUpload', ['$q', '$resource', '$http', function ($q, $resource, $http) {

        // 上传图片
        this.uploadFileToUrl = function (file, filechange,progresstest) {
            var fd = new FormData();
            if(filechange){
                fd.append("file", file, filechange);
            }else{
                fd.append("file", file);
            }
            fd.append("bizcode", "food");
            var deferred = $q.defer();
            $http({
                method: "POST",
                url: uploadUrl,
                uploadEventHandlers: {
                    progress: function(e) {
                        progresstest(e);
                    }
                },
                data: fd,
                headers: {'Content-Type': undefined},
                transformRequest: angular.identity,
            }).then(function (data) {
                deferred.resolve(data);
            },function (error) {
                deferred.reject(error);
            });
            return deferred.promise;
        };

        // 设置（读取）文件缓存
        var cookFile;

        this.setFile = function (file) {
            cookFile = file;
        };

        this.getFile = function () {
            return cookFile;
        };

        //
        var cookUrl;
        this.setCookUrl = function (file) {
            cookUrl = file;
        };

        this.getCookUrl = function () {
            return cookUrl;
        };

        // 二维码解析
        this.qrDecode = function(url,sucF,errF){
            var resource = $resource("http://inner.test.shuwang.info/common/qrcode/qrcode");
            return resource.save(url,sucF,errF);
        }
    }]);
;